//
//  Poem.m
//  FuncFanc
//
//  Created by hubing on 2018/4/16.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "Poem.h"

@implementation Poem
-(instancetype)init{
    if (self = [super init]) {
//        [self tangPoem];
//        [self songPoem];
//        [self songCi];
//        [self title];
//        [self paragraphs];
//        [self strains];
//        [self author];
    }
    return self;
}

-(void)loadPoem
{
   
}

-(NSMutableArray *)tangPoem{
    _tangPoem = [[NSMutableArray alloc]initWithCapacity:0];
    for (int i = 0 ; i <= 57000; i+=1000 ) {
        
        NSString *path = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"poet.tang.%d.json",i]];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        [_tangPoem addObjectsFromArray:array];
    }
    return _tangPoem;
}

-(NSMutableArray *)songPoem{
    _songPoem = [[NSMutableArray alloc]initWithCapacity:0]; // 254000
    for ( int i = 0 ; i <= 254000; i+=1000) {
        NSString *path = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"poet.song.%d.json",i]];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        [_songPoem addObjectsFromArray:array];
    }
    return _songPoem;
}

-(NSMutableArray *)songCi{
    _songCi = [[NSMutableArray alloc]initWithCapacity:0];
    for (int i = 0; i <=21000 ; i+=1000) {
     NSString *path = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"ci.song.%d.json",i]];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSData *data = [[NSData alloc]initWithContentsOfURL:url];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        [_songCi addObjectsFromArray:array];
    }
    return _songCi;
}


-(NSArray*)searchPoeByAuthor:(NSString *)searchWord{
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
    for (int i = 0 ; i < _tangPoem.count; i++) {
        NSString *author = _tangPoem[i][@"author"];
        if ([author isEqualToString:searchWord]) {
            [array addObject:_tangPoem[i]];
        }
    }
    return [array copy];
}

-(NSArray *)searchPoemTitleByAuthor:(NSString *)searchWord{
    NSMutableArray * titleArray = [[NSMutableArray alloc]initWithCapacity:0];
    NSArray *array = [self searchPoeByAuthor:searchWord];
    for (int i = 0 ; i < array.count; i ++) {
        NSString *poemTitle = array[i][@"title"];
        [titleArray addObject:poemTitle];
    }
    return [titleArray copy];
}



//-(NSString *)title{
//    _title = _tangPoem[_index][@"title"];
//    return _title;
//}
//
//-(NSArray *)paragraphs{
//    _paragraphs = _tangPoem[_index][@"paragraphs"];
//    return _paragraphs;
//}
//
//-(NSArray *)strains{
//    _strains = _tangPoem[_index][@"strains"];
//    return _strains;
//}
//
//-(Author *)author{
//   _author = [[Author alloc]initWithName:_tangPoem[_index][@"author"]];
//    return _author;
//}
//
//-(NSString *)authorName{
//    _authorName = _tangPoem[_index][@"author"];
//    return _authorName;
//}


-(void)ReadPoemFromBundle{
    NSString *path = [[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:@"poet.song.0.json"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
  //  NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
   
    _tangPoem = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    
    _title = _tangPoem[0][@"title"];
    _paragraphs = _tangPoem[0][@"paragraphs"];
    _strains = _tangPoem[0][@"strains"];
    _author = [[Author alloc]initWithName:_tangPoem[0][@"author"]];
    
}



@end
